<?
/*
Class Summary:
This class will provide methods to easily append totals to a data grid.
My personal suggestion will be to leave this as a core class and use some decorators
to print the output to the screen.
*/
class Grid{
	var $grid = array();
	var $Nrows = 0;
	
	function SpecialRowTotal(&$row, $keys_array){

			foreach($keys_array as $key)
				$sum += $row[$key];			
				
			array_push($row,$sum);						
	}#specialRowTotal
	
	function AppendSpecialRowsTotal($keys_array){
	
		foreach($this->grid as $key => $value)
			$this->SpecialRowTotal($this->grid[$key], $keys_array);			
	}#AppendSpecialRowsTotal
	
	function AppendRowsTotal(){

		foreach($this->grid as $key => $value)
			$this->AppendRowTotal($this->grid[$key]);			
	}
	
	function AppendRow($row){
		array_push($this->grid, $row);
		$this->Nrows++;
	}
	
		
	function AppendRowTotal(&$row){
		$sum = 0;
		foreach($row as $value)
			$sum += $value;
		array_push($row,$sum);
	}
	
	function AppendColumnTotal($columnIndex){
		$sum = 0; 
		$i = 0;
		

		foreach($this->grid as $row){
			if(isset($row[$columnIndex])) 		$i++;
	
			$sum += $row[$columnIndex];

		}#foreach
		$this->grid[$i][$columnIndex] = $sum;

	}
	
	function Walk(){
		foreach($this->grid as $row){
			foreach($row as $data){
				echo $data. " ";
			}	echo "<br/>";
		}	
	}#end Walk
	
	function LastRowIndex(){
		$this->LAST_ROW = count($this->grid)-1;
		return  $this->LAST_ROW;
	}#end LastRowIndex
	
	
}#end Grid
/*
$g = new Grid();
$g->grid[0][0] = 1;
$g->grid[0][1] = 2;
$g->grid[1][0] = 3;
$g->grid[1][1] = 4;

$g->AppendRowTotal($g->grid[0]);
$g->AppendRowTotal($g->grid[1]);

$g->AppendColumnTotal(0);
$g->AppendColumnTotal(1);
$g->AppendColumnTotal(2);

$g->walk();
*/
?>
